package com.cat.slidingWindow;

import java.util.Arrays;

/**
*   @description https://leetcode.cn/problems/the-k-strongest-values-in-an-array/description/
*   @author 曲大人的喵
*   @create 2025/9/17 21:37
*   @since JDK17
*/

public class Solution42 {
    public int[] getStrongest(int[] arr, int k) {
        Arrays.sort(arr);
        int[] ans = new int[k];
        int cnt = 0, m = arr[(arr.length - 2) / 2];
        for (int l = 0, r = arr.length - 1; l < r; ) {
            if (Math.abs(arr[l] - m) > Math.abs(arr[r] - m)) {
                ans[cnt++] = arr[l++];
            } else {
                ans[cnt++] = arr[r--];
            }
        }

        return ans;
    }
}
